#include <stdio.h>
#include <gdbm/ndbm.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>


typedef struct m_person{
	char name[15];
	int age;
}person;

int main(){
	DBM* dbmp=dbm_open("./test.dbm",O_RDWR | O_CREAT ,0666);
	if(!dbmp){
		perror("open error");
		exit(-1);
	}
	datum key_datum;
	datum data_datum;
	char key[20];
	sprintf(key,"id %d",12);
	key_datum.dptr=key;
	key_datum.dsize=sizeof(key);
	person p={"bluebit",26};
	srand(time());
	p.age=random()%20;
	data_datum.dptr=(void*)&p;
	data_datum.dsize=sizeof(person);
	int result=dbm_store(dbmp,key_datum,data_datum,DBM_REPLACE);
	if(result != 0){
		fprintf(stderr,"dbm store failed on  key%s\n",key_datum.dptr);
		exit(2);
	}
	datum dt=dbm_fetch(dbmp,key_datum);
	person* np=(person*)dt.dptr;
	printf("find %s , %d\n",np->name,np->age);

	dbm_close(dbmp);
}
